<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        var Pet = function (nickname, gender, owner, state = 80) {
            this.nickname = nickname
            this.gender = gender
            this.state = state
            this.owner = owner
            this.lastEatTime = new Date()
        }

        pet.prototype.introduceSelf = function () {
            console.log(`
            大家好，我是一只叫${this.nickname}的${this.type},
            我是个${this.gender === "1" ? "男" : "女"}生，
            我的主人是${this.owner},
            我的状态是${this.state} `)

        }


        pet.prototype.eat = function () {

            //十秒间隔
            var now = new Date()

            if (now - this.lastEatTime < 10 * 1000) {
                console.log('我吃的太频繁啦。目前不需要吃东西哦')

            } else {
                this.state += 20
                console.log(`嘻嘻，${this.nickname}吃饭了，当前状态${this.state},今天也要冲呀`)
                this.lastEatTime = new Date()
            }
        }

        var Dog = function () {
            this.type = "狗狗"
            pet.apply(this, arguments)
        }

        Dog.prototype = new Pet()
        DOg.prototype.play = function () {
            this.state += 10
            console.lo(` 嘻嘻，${this.nickname}最喜欢玩飞盘啦，当前状态${this.state},今天也要冲呀`)
        }

        var Cat = function () {
            this.type = "猫咪"
            Pet.apply(this, arguments)
        }
        Cat.prototype = new Pet()
        Cat.prototype.play = function () {
            this.state += 10
            console.log(`嘻嘻，${this.nickname}最喜欢玩毛线球啦,当前状态${this.state}`)
        }

        var Fish = function () {
            this.type = "鱼"
            Pet.apply(this, arguments)
        }
        Fish.prototype = new Pet()
        Fish.prototype.play = function () {
            this.state += 10
            console.log(`嘻嘻，${this.nickname}最喜欢仰泳啦,当前状态${this.state}`)
        }


        var User = function (nickname, password) {
            this.nickname = nickname
            this.password = password
        }


        var game = {
              //当前登录用户
              currentUser: null,
            //宠物数组
            petList: [new Pet("欢欢", "1", "ace123")],
            badWord: ["日本", "土共", "赤匪"],
            judgeBadWord(str) {
                for (let i = 0; i < this.badWord.length; i++) {
                    var keyword = this.badWord[i]
                    if (str.indexOf(keyword) > -1) {
                        return true
                    }
                }
                return false
            },
            userList: [new User("ace", '123456'), new User('robin', '123456')],
            // register: functhion(nickname, password){
            //     //检测是否包括敏感词

            //     if(this.judgeBadWord(nickname)){
            //         console.log(`您的昵称包含敏感词汇，请重新输入`)
            //         return false
            //         }

            //         //检测用户是否存在
            //         for (let i = 0; i < this.userList.length; i++) {
            //             const item = this.userList[i];
            //             if(item.nickname === nickname){
            //                 console.log('昵称已被注册，请重新输入')
            //             return false
            //             }


            //         }
            //         //创建对象，加入到用户数组中
            //         var user = new User (nickname,password)
            //         this.userList.push(user)
            //         return true
            //     }


            register: function (nickname, password) {
                //检测是否包含敏感词
                if (this.judgeBadWord(nickname)) {
                    console.log('您的昵称包含敏感词汇，请重新输入')
                    return false
                }

                //检测用户是否存在
                for (let i = 0; i < this.userList.length; i++) {
                    const item = this.userList[i];
                    if (item.nickname === nickname) {
                        console.log('昵称已被注册，请重新输入')
                        return false
                    }
                }

                //创建对象，加入到用户数组中
                var user = new User(nickname, password)
                this.userList.push(user)
                return true
            },
            login: function (nickname, password) {
                //检测用户是否存在
                for (let i = 0; i < this.userList.length; i++) {
                    const item = this.userList[i]
                    if (item.nickname === nickname && item.password === password) {
                        return true
                    }
                }
                return false

            },
            showRegisterPage: function () {
                console.log("注册")
                do {
                    var nickname = prompt("请输入昵称")
                } while (nickname.trim().length === 0)

                do {
                    var password = prompt("请输入密码")
                } while (password.trim().length < 6)

                do {
                    var password_confirm = prompt("请再次输入密码")
                } while (password_confirm !== password)
                if (this.register(nickname, password)) {
                    console.log("注册成功，当前系统共有" + this.userList.length + "位用户")
                    this.showWelcomePage()
                } else {
                    console.log("注册失败")
                    this.showRegisterPage()
                }

            },
            showLoginPage: function () {
                console.log("登录")
                do {
                    var nickname = prompt("请输入昵称")
                } while (nickname.trim().length === 0)
                do {
                    var password = prompt("请输入密码")

                } while (password.trim().length < 6)


                if (this.login(nickname.password)) {
                    console.log("登录成功")
                    this.currentUser = nickname
                    this.showMainPage()
                } else {
                    console.log("登录失败。用户名或者密码错误")
                    this.showWelcomePage()
                }
            },
            buyPet: function (nick, gender, type) {
                if (this.judgeBadWord(nick)) {
                    console.log(`您宠物的昵称包含敏感词汇，请重新输入`)
                    return false
                }

                var pet = null
                switch (type) {
                    case "1":
                        pet = new Dog(nickname, gender, this.currentUser)
                        break;
                    case "2":
                        pet = new Cat(nickname, gender, this.currentUser)
                        break;
                    case "3":
                        pet = new Fish(nickname, gender, this.currentUser)
                        break;

                }
                this.petList.push(pet)
                return true
            },
            showBuyPetPage: function () {
                var hasPet = false
                var myPetGender = '他'
                for (let i = 0; i < this.petList.length; i++) {
                    if (this.petList[i].owner === this.currentUser) {
                        hasPet = true
                        myPetGender = this.petList[i].gender === "1" ? "他" : "她"
                        break
                    }
                }
                if (hasPet) {
                    console.log('您已经有宠物了！请照顾好' + myPetGender)
                } else {
                    do {
                        var type = prompt("请选择宠物类型：1:狗狗 2:猫咪 3:鱼")
                    } while (gender.trim() !== "1" && gender.trim() !== "2")

                    do {
                        var nickname = prompt("请输入宠物昵称")
                    } while (nickname.trim().length === 0)

                    if (this.buyPet(nickname, gender, type)) {
                        console.log("领养成功 ")
                    }
                }

                this.showMainPage()
            },
            showMyPetPage: function () {
                var mypet = null
                for (let i = 0; i < this.petList.length; i++) {
                    if (this.petList[i].owner === this.currentUser) {
                        mypet = this.petList[i]
                        break
                    }
                }

                if (mypet) {
                    mypet.introduceSelf()
                    this.showPlayWithPetPage(mypet)
                } else {
                    console.log("您还没有宠物！")
                    this.showMainPage()
                }
            },
            showPlayWithPetPage(mypet) {
                var result = prompt("1.喂食   2.逗TA开心   3.回到主菜单")
                switch (result) {
                    case "1":
                        mypet.eat()
                        this.showPlayWithPetPage(mypet)
                        break;
                    case "2":
                        mypet.play()
                        this.showPlayWithPetPage(mypet)
                        break;
                    case "3":
                        this.showMainPage()
                        break;
                    default:
                        console.log("输入有误")
                        arguments.callee.call(this, mypet)
                }
            },
            showMainPage: function () {
                var result = prompt("1.领养宠物   2.我的宠物   3.退出")
                switch (result) {
                    case "1":
                        this.showBuyPetPage()
                        break;
                    case "2":
                        this.showMyPetPage()
                        break;
                    case "3":
                        this.showWelcomePage()
                        break;
                    default:
                        console.log("输入有误")
                        arguments.callee.call(this)
                }
            },
            showWelcomePage: function () {
                var result = prompt("1.注册   2.登录   3.退出")
                switch (result) {
                    case "1":
                        this.showRegisterPage()
                        break;
                    case "2":
                        this.showLoginPage()
                        break;
                    case "3":
                        console.log("退出")
                        break;
                    default:
                        console.log("输入有误")
                        arguments.callee.call(this)
                        break;
                }
            }
        }
        game.showWelcomePage()
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
     



















    </script>

</body>

</html>